package com.runfast.waimai.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ComUtil {

	private static final int KB = 1024;
	private static final int MB = 1024*1024;

	/**
	 * 格式化字节单位
	 * @param bytes
	 * @return
	 */
	public static String formatByte(long bytes){

		DecimalFormat df = new DecimalFormat("0.0");
		StringBuffer sbuf = new StringBuffer();
		if(bytes>=100*KB){
			sbuf.append(df.format(bytes*1.0f/MB)).append("M");
		}else{
			sbuf.append(df.format(bytes*1.0f/KB)).append("K");
		}
		return sbuf.toString();
	}

	public static Date addDay(Date date,int day){

		Calendar cl = Calendar.getInstance();
		cl.setTime(date);
		cl.add(Calendar.DAY_OF_YEAR, day);

		return cl.getTime();
	}

	/**
	 * 格式化
	 * @param bytes
	 * @return
	 */
	public static String formatByte2(long bytes){

		DecimalFormat df = new DecimalFormat("0.00");
		StringBuffer sbuf = new StringBuffer();
		if(bytes>=600*KB){
			sbuf.append(df.format(bytes*1.0f/MB)).append("M");
		}else{
			sbuf.append(df.format(bytes*1.0f/KB)).append("K");
		}
		return sbuf.toString();
	}

	/**
	 * 格式化
	 * @param bytes
	 * @return
	 */
	public static String formatByte3(long bytes){
		DecimalFormat df = new DecimalFormat("0.00");
		StringBuffer sbuf = new StringBuffer();
		sbuf.append(df.format(bytes*1.0f/MB));
		return sbuf.toString();
	}

	/**
	 * 日期是否大于今天(含今天)
	 * @param date
	 * @return
	 */
	public static boolean afterToday(Date date){

		boolean after = false;
		Calendar curDate = Calendar.getInstance();
		curDate.add(Calendar.DAY_OF_YEAR, -1);
		if(curDate.getTime().before(date)){
			after = true;
		}
		return after;
	}

	/**
	 * 返回制定格式日期
	 * @param format
	 * @return
	 */
	public static String getDate(String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date());
	}

	/**
	 * 返回指定格式日期
	 * @param format
	 * @param date
	 * @return
	 */
	public static String getDate(String format,Date date){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	/**
	 * 对比两个long时间值的差距
	 * @param start
	 * @param end
	 * @return 返回x天x时x分x秒的格式
	 */
	public static String diffTime(long start,long end){

		String str = null;
		long diff = end - start;
		if(diff<=0){
			return "0天0时0分";
		}else{
			long day = 0;
			long hou = 0;
			long min = 0;
			//long ses = 0;
			day = diff/(1000*60*60*24);
			diff = diff - day*1000*60*60*24;
			hou = (diff/(1000*60*60));
			diff = diff - hou*1000*60*60;
			min = (diff/(1000*60));
			diff = diff - min*1000*60;
			//ses = (diff/1000);
			//str = String.format("%d天%d时%d分%d秒",day,hou,min,ses);
			str = String.format("%d天%d时%d分",day,hou,min);
		}

		return str;
	}

	/**
	 * 返回两个日期相差的天数
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int getDaysBetween(Date date1, Date date2) {
		Calendar cl1 = Calendar.getInstance();
		Calendar cl2 = Calendar.getInstance();
		cl1.setTime(date1);
		cl2.setTime(date2);
		if (cl1.after(cl2)) {
			Calendar swap = cl1;
			cl1 = cl2;
			cl2 = swap;
		}
		int days = cl2.get(Calendar.DAY_OF_YEAR) - cl1.get(Calendar.DAY_OF_YEAR);
		int y2 = cl2.get(Calendar.YEAR);
		if (cl1.get(Calendar.YEAR) != y2) {
			cl1 = (Calendar) cl1.clone();
			do {
				days += cl1.getActualMaximum(Calendar.DAY_OF_YEAR);
				cl1.add(Calendar.YEAR, 1);
			} while (cl1.get(Calendar.YEAR) != y2);
		}
		return days;
	}

	public static int getInt(String str){//把字符串转换为int类型
		int i = 0;
		try{
			i = Integer.parseInt(str);
		}catch(NumberFormatException ex){}
		return i;
	}

	public static double getDouble(String str){

		double d = 0;
		if(str!=null){
			try{
				d = Double.parseDouble(str);
			}catch(Exception e){
				e.printStackTrace();
			}
		}

		return d;
	}

	public static String getRex(String str, String regEx) {
		String tmp = null;
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		if (m.find()) {
			tmp = m.group();
		}
		return tmp;
	}

	public static List<String> getRexs(String str, String regEx) {

		List<String> list = new ArrayList<String>();
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		while (m.find()) {
			list.add(m.group());
		}
		return list;
	}

	public static boolean isRex(String str, String regEx) {
		boolean b = false;
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		if (m.find()) {
			b = true;
		}
		return b;
	}

	/**
	 * 字符是否为空
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str){
		if(str==null || str.length()==0){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 从字符串获取xx元中的xx
	 * @param str
	 * @return
	 */
	public static int getYuan(String str){
		int yuan = 0;
		if(str!=null && str.length()>0){
			String matchStr = ComUtil.getRex(str, "\\d+元");
			if(matchStr!=null){
				try{yuan = Integer.parseInt(matchStr.replace("元", ""));}catch(Exception ex){}
			}
		}
		return yuan;
	}

	public static String getMD5(String s){

		StringBuffer sbuf = new StringBuffer();
		try {
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(s.getBytes());
			byte[] buf = mdTemp.digest();
			for(byte b:buf){
				sbuf.append(Integer.toHexString(b>>4 & 0xF).toUpperCase());
				sbuf.append(Integer.toHexString(b & 0xF).toUpperCase());
			}
		} catch (NoSuchAlgorithmException ex) {
			ex.printStackTrace();
		}
		return sbuf.toString();
	}

}
